package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.MemberMapper;
import com.fantastic.model.pojo.zjs.Member;
import com.fantastic.service.zjs.MemberService;
import com.sun.org.apache.xpath.internal.operations.Mult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/4 16:08
 * @description: 会员卡信息service接口实现类
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    /**
     * 查询会员卡信息
     *
     * @param member
     * @return
     */
    @Override
    public List<Member> listMember(Member member) {
        List<Member> members = memberMapper.listMember(member);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), members);
        for (Member list : members) {
            list.setMemberPic(FilesUtils.getSlashParse(list.getMemberPic()));
        }
        return members;
    }

    /**
     * 查询单个会员卡信息
     *
     * @param memberId
     * @return
     */
    @Override
    public Member getMember(Integer memberId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), memberId);
        Member member = memberMapper.getMember(memberId);
        member.setMemberPic(FilesUtils.getSlashParse(member.getMemberPic()));
        return member;
    }

    /**
     * 添加会员卡信息
     *
     * @param member
     * @return
     */
    @Override
    public Integer insertMember(MultipartFile memberFile, Member member) {
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), member.getMemberName(), member.getMemberRemark());
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), member.getMemberState());
        IsEmptyUtils.objectIsNull(new InsertException(RespStatusInfo.DATA_ERROR), member.getMemberPrice());
        if (memberFile != null && memberFile.getSize() != 0) {
            MultipartFileUtils.supportType(memberFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(memberFile, "广告");
            member.setMemberPic(relativePath);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员卡信息
     *
     * @param member
     * @return
     */
    @Override
    public Integer updateMember(MultipartFile memberFile, Member member) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), member.getMemberId());
        //先查询原来的会员卡路径
        if (memberFile != null && memberFile.getSize() != 0) {
            MultipartFileUtils.supportType(memberFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(memberFile, "广告");
            member.setMemberPic(relativePath);
            //先查询原来的会员卡路径
            Member getMember = getMember(member.getMemberId());
            MultipartFileUtils.deleteFile(FilesUtils.getBackslashParse(getMember.getMemberPic()));
        }
        return memberMapper.updateMember(member);
    }

    /**
     * 删除会员卡信息
     *
     * @param memberId
     * @return
     */
    @Override
    public Integer deleteMember(Integer memberId) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), memberId);
        return memberMapper.deleteMember(memberId);
    }

}
